package com.stockz.core.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.stockz.core.model.Bar;
import com.stockz.core.util.BarUtil.BarValueType;

public class ListUtil {

	public static double[] convert(List<Bar> barList,BarValueType valueType){
		if(barList == null){
			return new double[0];
		}
		double[] values = new double[barList.size()];
		for(int i = 0; i < barList.size(); i++){
			values[i] = BarUtil.getValue(barList.get(i), valueType);
		}
		return values;
	}
	
	
	public static double[] convert(List<Bar> barList,BarValueType valueType,int span){
		if(barList == null || barList.size() < span){
			return new double[0];
		}
		double[] values = new double[span];
		for(int i = span-1; i >= 0; i--){
			values[i] = BarUtil.getValue(barList.get(i), valueType);
		}
		return values;
	}
	
	public static double[] convert(List<Bar> barList, BarValueType type, int start, int end){
		if(barList != null && start >= 0 && end >= start && barList.size() > end){
			double[] values = new double[end - start +1];
			for(int i = start, j = 0; i <= end; i++,j++){
				values[j] = BarUtil.getValue(barList.get(i), type);
			}
			return values;
		}
		return new double[0];
	}
	
	
	public static List<Bar> getExtreams(List<Bar> list, double per,BarValueType type){
		if(list != null && list.size() != 0){
			Bar priorLast = list.get(list.size()-1);
			Bar last = priorLast;
			double validShift = BarUtil.getValue(last, type)*per/100;
			List<Bar> highs = new ArrayList<Bar>();
			List<Bar> lows = new ArrayList<Bar>(); 
			for(int i = list.size()-1; i >=0; i--){
				Bar current = list.get(i);
				 if(Math.abs(BarUtil.getValue(current,type)-BarUtil.getValue(last,type)) >= validShift){
					 if(BarUtil.getValue(priorLast,type) < BarUtil.getValue(last,type) &&	 BarUtil.getValue(current,type) < BarUtil.getValue(last,type)){
						 highs.add(last);
					 }
					 if(BarUtil.getValue(priorLast,type) > BarUtil.getValue(last,type) &&	 BarUtil.getValue(current,type) > BarUtil.getValue(last,type)){
						 lows.add(last);
					 }
					 priorLast = last;
					 last = current;
					 validShift = BarUtil.getValue(last,type)*per/100;
				 }else{
					 if(BarUtil.getValue(priorLast,type) > BarUtil.getValue(last,type) &&	 BarUtil.getValue(current,type) < BarUtil.getValue(last,type)){
						 last = current;
					 }else if(BarUtil.getValue(priorLast,type) < BarUtil.getValue(last,type) && BarUtil.getValue(current,type) > BarUtil.getValue(last,type)){
						 last = current;
					 }
				 }
			}
			
			/*Bar bar = null;
			
			if(lows.contains(last)){
				for(int i = 0; i < list.indexOf(last); i++){
					
				}
			}else if(highs.contains(last)){
				
			}*/
			
			
			highs.addAll(lows);
			Collections.sort(highs);
			return highs;
		}else 
			return null;
	}
	
	
}
